package swu.cx.finalproject


import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.animation.PropertyValuesHolder
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.text.Editable
import android.text.SpannableStringBuilder
import android.text.TextWatcher
import android.util.DisplayMetrics
import android.util.Log
import android.view.View
import kotlinx.android.synthetic.main.activity_login.*
import kotlinx.android.synthetic.main.activity_main.*

class LoginActivity : AppCompatActivity(),TextWatcher, View.OnFocusChangeListener {
    private val pwd by lazy {
        SharedPreferenceUtil.getInstance(this).GetPasswordInfo()
    }
    override fun onFocusChange(p0: View?, p1: Boolean) {
        if(p0 == password){
            if(p1){
                covereyes()
            }else{
                openeyes()
            }
        }
    }

    override fun afterTextChanged(p0: Editable?) {
        login.isEnabled = (username.text.isNotEmpty() && password.text.isNotEmpty())
    }

    override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

    }

    override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

    }

    private val lefthanddown :ObjectAnimator by lazy{
        ObjectAnimator.ofFloat(lh,"translationY",lh.height*0.5f).apply {
            duration = 1000
        }
    }//左手掌下移

    private val lefthandUp :ObjectAnimator by lazy{
        ObjectAnimator.ofFloat(lh,"translationY",0f).apply {
            duration = 1000
        }
    }//左手掌上移

    private val righthanddowm :ObjectAnimator by lazy{
        ObjectAnimator.ofFloat(rh,"translationY",rh.height*0.5f).apply {
            duration = 1000
        }
    }//右手掌下移

    private val righthandUp :ObjectAnimator by lazy{
        ObjectAnimator.ofFloat(rh,"translationY",0f).apply {
            duration = 1000
        }
    }//右手掌上移

    private val leftArmUp : AnimatorSet by lazy{
       val rotate = ObjectAnimator.ofFloat(la,"rotation",140f).apply{
        duration = 1000
    }
        val tx = PropertyValuesHolder.ofFloat("translationX",dptopx(48f))
        val ty = PropertyValuesHolder.ofFloat("translationY",-dptopx(40f))
        val translate = ObjectAnimator.ofPropertyValuesHolder(la,tx,ty)
        AnimatorSet().apply {
            playTogether(rotate,translate)
            duration = 1000
        }
    }//左手臂向上

    private val leftArmdown : AnimatorSet by lazy{
        val rotate = ObjectAnimator.ofFloat(la,"rotation",0f).apply{
            duration = 1000
        }
        val tx = PropertyValuesHolder.ofFloat("translationX",0f)
        val ty = PropertyValuesHolder.ofFloat("translationY",0f)
        val translate = ObjectAnimator.ofPropertyValuesHolder(la,tx,ty)
        AnimatorSet().apply {
            playTogether(rotate,translate)
            duration = 1000
        }
    }//左手臂向下

    private val rightArmUp : AnimatorSet by lazy{
        val rotate =  ObjectAnimator.ofFloat(ra,"rotation",-140f).apply{
            duration = 1000
        }
        val tx = PropertyValuesHolder.ofFloat("translationX",-dptopx(48f))
        val ty = PropertyValuesHolder.ofFloat("translationY",-dptopx(40f))
        val translate = ObjectAnimator.ofPropertyValuesHolder(ra,tx,ty)
        AnimatorSet().apply {
            playTogether(rotate,translate)
            duration = 1000
        }

    }//右手臂向上

    private val rightArmdown : AnimatorSet by lazy{
      val rotate =  ObjectAnimator.ofFloat(ra,"rotation",0f).apply{
            duration = 1000
        }
        val tx = PropertyValuesHolder.ofFloat("translationX",0f)
        val ty = PropertyValuesHolder.ofFloat("translationY",0f)
        val translate = ObjectAnimator.ofPropertyValuesHolder(ra,tx,ty)
        AnimatorSet().apply {
            playTogether(rotate,translate)
            duration = 1000
        }

    }//右手臂向下

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_login)
        username.addTextChangedListener(this)
        password.addTextChangedListener(this)
        username.onFocusChangeListener = this
        password.onFocusChangeListener = this
        val content=SharedPreferenceUtil.getInstance(this).GetUserInfo()
        username.text = if(content==null) SpannableStringBuilder("您当前没有账号!!") else SpannableStringBuilder(content)
        regist.setOnClickListener{
            Intent(this@LoginActivity,RegisterActivity::class.java).apply {
                startActivity(this)
            }
        }//跳转到注册界面

        login.setOnClickListener {
            if(password.text.toString()==pwd){
                startActivity(Intent(this,LeftContact::class.java))
            }else{
                regist.text="密码错误，请重新输入！！"
            }
        }//跳转到列表界面
    }

    fun covereyes(){
        AnimatorSet().apply {
            play(lefthanddown)
                .with(righthanddowm)
                .before(leftArmUp)
                .before(rightArmUp)
            start()
        }
    }//遮住眼睛
    fun openeyes(){
        AnimatorSet().apply {
            play(leftArmdown).with(rightArmdown).before(lefthandUp).before(righthandUp)
            start()
        }
    }//打开眼睛

    override fun onStart() {
        super.onStart()

        blur.startBlur()
    } //开始虚化

    override fun onResume() {
        super.onResume()
        blur.pauseBlur()
    }

    fun dptopx(dp:Float):Float{
        val display = DisplayMetrics()
        windowManager.defaultDisplay.getMetrics(display)
        return display.density*dp
    }//将dp值转化为像素
}
